﻿// Decompiled with JetBrains decompiler
// Type: System.Web.Razor.Parser.ConditionalAttributeCollapser
// Assembly: System.Web.Razor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
// MVID: 682513D6-F37E-48AC-9909-F3EE103CFF14
// Assembly location: D:\Project\Idea\无人机外网\uav\客户端工程\排查工具\bin\Release\System.Web.Razor.dll

using System.Collections.Generic;
using System.Linq;
using System.Web.Razor.Editor;
using System.Web.Razor.Generator;
using System.Web.Razor.Parser.SyntaxTree;
using System.Web.Razor.Text;
using System.Web.Razor.Tokenizer;
using System.Web.Razor.Tokenizer.Symbols;

namespace System.Web.Razor.Parser
{
  internal class ConditionalAttributeCollapser : MarkupRewriter
  {
    public ConditionalAttributeCollapser(
      Action<SpanBuilder, SourceLocation, string> markupSpanFactory)
      : base(markupSpanFactory)
    {
    }

    protected override bool CanRewrite(System.Web.Razor.Parser.SyntaxTree.Block block) => block.CodeGenerator is AttributeBlockCodeGenerator && block.Children.Any<SyntaxTreeNode>() && block.Children.All<SyntaxTreeNode>(new Func<SyntaxTreeNode, bool>(this.IsLiteralAttributeValue));

    protected override SyntaxTreeNode RewriteBlock(BlockBuilder parent, System.Web.Razor.Parser.SyntaxTree.Block block)
    {
      string content = string.Concat(block.Children.Cast<Span>().Select<Span, string>((Func<Span, string>) (s => s.Content)));
      SpanBuilder builder = new SpanBuilder();
      builder.EditHandler = new SpanEditHandler(new Func<string, IEnumerable<ISymbol>>(HtmlTokenizer.Tokenize));
      this.FillSpan(builder, block.Children.Cast<Span>().First<Span>().Start, content);
      return (SyntaxTreeNode) builder.Build();
    }

    private bool IsLiteralAttributeValue(SyntaxTreeNode node)
    {
      if (node.IsBlock)
        return false;
      Span span = node as Span;
      LiteralAttributeCodeGenerator codeGenerator = span.CodeGenerator as LiteralAttributeCodeGenerator;
      if (span == null)
        return false;
      return codeGenerator != null && codeGenerator.ValueGenerator == (LocationTagged<SpanCodeGenerator>) null || span.CodeGenerator == SpanCodeGenerator.Null || span.CodeGenerator is MarkupCodeGenerator;
    }
  }
}
